/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package model.dao;

import lib.*;
import model.dao.vo.*;

import java.sql.*;
import java.util.*;

/**
 *
 * @author felipe
 */
public class CinemaDAO extends DAO {

    public CinemaDAO()
    {
        super();
    }

    public Iterator<CinemaVO> listaCinema(CinemaVO cinema)
    {
        SQL sql = Factory.createSQL();
        sql = sql.select("cinema_id, cinema_nome")
                .from("cinema")
                .where("cinema_nome LIKE '%" + cinema.getNome() + "%'");
        ResultSet rs = this.db.Execute(sql.toString());
        try
        {
            if ( ! rs.wasNull() )
            {
                List<CinemaVO> cinemas = new ArrayList<CinemaVO>();
                while(rs.next())
                {
                    CinemaVO c = new CinemaVO();
                    c.setId(rs.getInt("cinema_id"));
                    c.setNome(rs.getString("cinema_nome"));
                    cinemas.add(c);
                }

                if ( ! cinemas.isEmpty()){
                    return (Iterator<CinemaVO>) cinemas.iterator();
                }
            }
        }
        catch( SQLException e )
        {
            ErrorLog.SQLLog("Error listaPessoa method " , e);
        }
        return null;
    }

    public CinemaVO buscaCinema(CinemaVO cinema)
    {
        SQL sql = Factory.createSQL();
        sql = sql.select("cinema_nome")
                .from("cinema")
                .where("cinema_id = " + cinema.getId());
        ResultSet rs = this.db.Execute(sql.toString());
        try
        {
            if ( ! rs.wasNull() )
            {
                cinema.setId(rs.getInt("cinema_id"));
                cinema.setNome(rs.getString("cinema_nome"));
                return cinema;
            }
        }
        catch( SQLException e )
        {
            ErrorLog.SQLLog("Error Main method " , e);
        }
        return null;
    }

    public boolean insereCinema(CinemaVO cinema)
    {
        SQL sql = Factory.createSQL();
        sql = sql.insert()
                .into( "cinema","cinema_id, cinema_nome")
                .value("NULL,'"+cinema.nome+"'");
        int sucess = this.db.Update(sql.toString());
        if (sucess == 1){
            return true;
        }
        return false;
    }

    public boolean alteraCinema(CinemaVO cinema)
    {
        SQL sql = Factory.createSQL();
        sql.update("cinema")
                .set("cinema_nome = '" + cinema.nome + "'")
                .where("cinema_id = " + cinema.id);
        int sucess = this.db.Update(sql.toString());
        if (sucess > 0 ){
            return true;
        }
        return false;
    }

    public boolean apagaCinema(CinemaVO cinema)
    {
        SQL sql = Factory.createSQL();
        sql = sql.delete("cinema")
                .where("cinema_id = " + cinema.getId());
        int sucess = this.db.Update(sql.toString());
        if (sucess > 0 ){
            return true;
        }
        return false;
    }

}
